package en.kaist.cs408.acav;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import en.kaist.cs408.R;

//L'adapter associ� au ListView
public class MyAdapter extends ArrayAdapter<Student> {

	private List<Student> studentList;
	private Context context;

	//constructeur de Adapter
	public MyAdapter(List<Student> stringList, Context ctx) {
		super(ctx, R.layout.row_layout, stringList);
		this.studentList = stringList;
		this.context = ctx;
	}

	//ajoute une ligne � la liste
	public void add(Student elmt) {
		studentList.add(elmt);
	}
	
	public void setStudentList(List<Student> listStud)
	{
		this.studentList = listStud;
	}

	//retourne le nombre d'�l�ments de la liste
	public int getCount() {
		return studentList.size();
	}

	//r�cup�re la ligne dans la liste selon l'index prix en param�tre
	public Student getItem(int position) {
		return studentList.get(position);
	}

	//get the line according to line hascode
	public long getItemId(int position) {
		return studentList.get(position).hashCode();
	}

	//return the view associated to the adapter
	public View getView(int position, View convertView, ViewGroup parent) {
					LayoutInflater inflater = (LayoutInflater) context
					        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					    View rowView = inflater.inflate(R.layout.row_layout, parent, false);
					
			// remplit la view avec les nouvelles valeurs
			TextView tv = (TextView) rowView.findViewById(R.id.item_student);
			ImageView img = (ImageView) rowView.findViewById(R.id.icon);
			
			tv.setText(studentList.get(position).getId());
			
			/*set the correct image for student attendance*/
			boolean attValue = studentList.get(position).getAttendance();
			if (attValue) {
			      img.setImageResource(R.drawable.ok);
			    } else {
			      img.setImageResource(R.drawable.no);
			    }
			if (position % 2 == 0){
				tv.setBackgroundResource(R.color.transparent);
				img.setBackgroundResource(R.color.transparent);
			}
			else{
				tv.setBackgroundResource(R.color.altercolor2);
				img.setBackgroundResource(R.color.altercolor2);
			}


		return rowView;
	}
	
//	 public void testData()
//	    {
//	    	studentList.add(new Student("20146320",false));
//	    	studentList.add(new Student("20071143",true));
//	    	studentList.add(new Student("20070010",false));
//	    	studentList.add(new Student("20123456",false));
//	    }

	private static class StringHolder {
		public TextView StringNameView;
	}

}
